package utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.DecimalFormat;
import java.util.Collections;
import java.util.HashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import jobqueue.JobQueueImpl;
import jobqueue.Scheduler;


import data.ClusterInfo;
import data.UserInfo;

public class Utils {
	public static JobQueueImpl jobq = new JobQueueImpl();
	public static Scheduler sched = new Scheduler(jobq);
	public static ExecutorService exec = Executors.newCachedThreadPool(); 
	public static DecimalFormat f = new DecimalFormat("##.00"); 
	public static String db_dvr = "com.mysql.jdbc.Driver";
	public static String db_url = "jdbc:mysql://127.0.0.1/sire";
	public static String db_usr = "root";
	public static String db_pwd = "";
	static {
		try {
			
			new Thread(sched).start();
			Class.forName(db_dvr);
			Connection conn = DriverManager.getConnection(db_url, db_usr, db_pwd);
			String query = "SELECT * FROM users";
			PreparedStatement stmt = conn.prepareStatement(query);
			ResultSet rs = stmt.executeQuery();
			while (rs.next()) {
				String username = rs.getString("name");
				String passwd = rs.getString("pass");
				String email = rs.getString("email");
				int id = rs.getInt("id");
				UserInfo ui = new UserInfo(username, passwd, email, id);
				Database.users.put(username, ui);
				Database.clusters.put(username, Collections
						.synchronizedMap(new HashMap<String, ClusterInfo>()));
				Database.GetUserClusters(conn, id, username);
				Utils.out("ADDED " + username + " " + email + " to users table");
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(0);
		}
	}
	public static void out(Object o)
	{
		System.out.println(o.toString());
	}
	
	public static int ConvertWalltime(String w)
	{
		String [] a = w.split(":");
		return Integer.parseInt(a[2])+ Integer.parseInt(a[1])*60 + Integer.parseInt(a[0])*3600;
	}
}
